package com.example.myapplication.found;

import com.example.myapplication.found.bean.FoundBean;
import com.example.myapplication.shortvideo.utils.GsonUtil;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * @author: rongfeng
 * @date: 2022/1/25
 * @description
 */
public class Attention {
    int index, monthIndex, cValueIndex;
    List<FoundBean> foundList = new ArrayList<>();
    ExecutorService pool;
    int[] month;
    float[] cValue;

    public static void main(String[] args) throws Exception {
        Lsjz.isTest = false;
        Attention attention = new Attention();
        attention.month = attention.getMonth();
        attention.cValue = attention.getCvalue();
        attention.pool = Executors.newFixedThreadPool(5);
        attention.getFund("https://21.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112404882402502091565_1644305883838&pn=1&pz=700&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=b:MK0021,b:MK0022,b:MK0023,b:MK0024&fields=f12,f14&_=" + System.currentTimeMillis());
    }

    public float[] getCvalue() {
        float[] cValue = new float[10];
        float frist = 0.5f;
        for (int i = 0; i < cValue.length; i++) {
            DecimalFormat df2 = new DecimalFormat("0.0");
            frist = Float.parseFloat(df2.format(frist));
            cValue[i] = frist;
            frist = (frist + 0.5f);
        }
        return cValue;
    }

    public int[] getMonth() {
        int[] cValue = new int[24];
        for (int i = 0; i < cValue.length; i++) {
            cValue[i] = i + 2;
        }
        return cValue;
    }


    public void fenxi() {
        pool.execute(new Runnable() {
            @Override
            public synchronized void run() {
                if (index < foundList.size()) {
                    FoundBean item = foundList.get(index);
                    Lsjz lsjz = new Lsjz(item.f12, item.f14);
                    lsjz.setCallback(callback);
                    lsjz.start();
                    index++;
                } else {
                    //去重
                    List<Lsjz.Attention> attentions = new ArrayList<>();
                    for (int i = 0; i < attentionList.size(); i++) {
                        Lsjz.Attention item = attentionList.get(i);
                        boolean isRpit = false;
                        for (int j = i + 1; j < attentionList.size(); j++) {
                            Lsjz.Attention item2 = attentionList.get(j);
                            if (item.fundName.contains(item2.fundName) || item2.fundName.contains(item.fundName)) {
                                isRpit = true;
                            }
                        }
                        if (!isRpit) {
                            attentions.add(item);
                        }
                    }
                    for (Lsjz.Attention attention : attentions) {
                        System.out.println("值得关注 " + attention.fundName + " " + attention.fundCode);
                        for (String item : attention.des) {
                            System.out.println(item);
                        }
                        System.out.println();
                    }
                    System.out.println("共计 " + attentions.size());
                }
            }
        });
    }

    ZuiJia zuiJia = new ZuiJia();
    ZuiJia zuiJia2 = new ZuiJia();

    public void fenxi2() {
        pool.execute(new Runnable() {
            @Override
            public synchronized void run() {
                if (index < foundList.size()) {
                    FoundBean item = foundList.get(index);
                    Lsjz lsjz = new Lsjz(item.f12, item.f14);
                    lsjz.setCallback2(callback2);
                    lsjz.start();
                    index++;
                } else {
                    for (int i = 0; i < zdList.size(); i++) {
                        List<Float> dayAvgList = zdList.get(i);
                        float zhangfu = 0;
                        int pin = 0, zhang = 0, fu = 0;
                        for (int j = 0; j < dayAvgList.size(); j++) {
                            float zd = dayAvgList.get(j);
                            zhangfu = zhangfu + zd;
                            if (zd == 0) {
                                pin++;
                            } else if (zd > 0) {
                                zhang++;
                            } else {
                                fu++;
                            }
                        }

                        float rate = ((float) zhang / dayAvgList.size());
                        DayAvg dayAvg = new DayAvg();
                        dayAvg.day = i;
                        dayAvg.zhangfu = zhangfu;
                        dayAvg.fu = fu;
                        dayAvg.pin = pin;
                        dayAvg.zhang = zhang;
                        dayAvg.rate = rate;

                        if (zuiJia.dayAvgList.size() < i + 1) {
                            zuiJia.cValue = cValue[cValueIndex];
                            zuiJia.month = month[monthIndex];
                            zuiJia.dayAvgList.add(dayAvg);
                        } else {
                            DayAvg laDayAvg = zuiJia.dayAvgList.get(i);
                            if (rate > laDayAvg.rate) {
                                zuiJia.cValue = cValue[cValueIndex];
                                zuiJia.month = month[monthIndex];
                                zuiJia.dayAvgList.set(i, dayAvg);
                            }
                        }
                    }
                    System.out.println("间隔月 " + month[monthIndex] + " 相似差 " + cValue[cValueIndex]);
                    for (DayAvg dayAvg : zuiJia.dayAvgList) {
                        System.out.println(dayAvg.day + " 天 后 ");
                        System.out.println("涨 " + dayAvg.zhang + " 跌 " + dayAvg.fu + "  平 " + dayAvg.pin);
                        System.out.println("涨比率 " + dayAvg.rate);
                        System.out.println("涨幅 " + dayAvg.zhangfu);
                        System.out.println();
                    }
                    fenxi3();
                }
            }
        });
    }

    static class ZuiJia {
        int month;
        float cValue;
        List<DayAvg> dayAvgList = new ArrayList<>();
    }

    static class DayAvg {
        int day;
        int pin = 0, zhang = 0, fu = 0;
        float zhangfu;
        float rate;
    }

    public void fenxi3() {
        index = 0;
        zdList.clear();
        if (monthIndex < month.length - 1) {
            fenxi2();
            monthIndex++;
        } else if (cValueIndex < cValue.length - 1) {
            fenxi2();
            cValueIndex++;
            monthIndex = 0;
        } else {
            System.out.println("涨比率最佳 ");

            System.out.println("间隔月 " + month[monthIndex] + " 相似差 " + cValue[cValueIndex]);
            for (DayAvg dayAvg : zuiJia.dayAvgList) {
                System.out.println(dayAvg.day + " 天 后 ");
                System.out.println("涨 " + dayAvg.zhang + " 跌 " + dayAvg.fu + "  平 " + dayAvg.pin);
                System.out.println("涨比率 " + dayAvg.rate);
                System.out.println("涨幅 " + dayAvg.zhangfu);
                System.out.println();
            }
        }
    }


    List<Lsjz.Attention> attentionList = new ArrayList<>();
    Lsjz.FenxiCallback callback = new Lsjz.FenxiCallback() {
        @Override
        public void callback(Lsjz.Attention attention) {
            if (attention != null && !attention.des.isEmpty()) {
                attentionList.add(attention);
            }
            fenxi();
        }
    };

    List<List<Float>> zdList = new ArrayList<>();
    Lsjz.Fenxi2Callback callback2 = new Lsjz.Fenxi2Callback() {
        @Override
        public void callback(List<List<Float>> averagelist) {
            zdList.addAll(averagelist);
            fenxi2();
        }
    };


    public void getFund(String url) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
        final Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                if (response.body() != null) {
                    String html = response.body().string();
                    String tag1 = "\"diff\":";
                    int index1 = html.indexOf(tag1);
                    int index2 = html.lastIndexOf("}});");
                    html = html.substring(index1 + tag1.length(), index2);
                    List<FoundBean> bean = GsonUtil.parse(html, new TypeToken<List<FoundBean>>() {
                    }.getType());
                    foundList = bean;
                    fenxi2();
                }
            }
        });
    }


}
